import React from 'react';
import CountDown from "@/components/common/countdown"; // 引入封装好的组件
const Email = () => {

    const [form] = React.UI.Form.useForm()

    // 邮箱认证
    const OnSubmitEvent = async(values: any) => 
    {
        let result = await React.HTTP.upload('/business/email', {
            busid: React.Business.id,
            code: values.code
        })
        if (result.code == 1) 
        {
            React.success(result.msg,() => React.navigate('/business/index'))
        }
        else 
        {
            React.error(result.msg)
        }
    }

    // 发送邮件
    const SendEmail = async() =>
    {
        let result = await React.HTTP.upload('/business/sendemail', {
            busid: React.Business.id,
        })
        if (result.code == 1) 
        {
            React.success(result.msg)
        }
        else 
        {
            React.error(result.msg)
        }
    }

    return (
        <>
            <React.UI.Form
                form={form}
                layout='horizontal'
                onFinish={OnSubmitEvent}
                footer={
                    <React.UI.Button block type='submit' color='primary' size='large'>
                        提交修改
                    </React.UI.Button>
                }
            >
                {/* 邮箱 */}
                <React.UI.Form.Item name='email' label='邮箱' rules={[{ required: true, message: '请填写邮箱' }, { type: 'email', warningOnly: true }]} initialValue={React.Business.email}>
                    <React.UI.Input placeholder='请输入邮箱' />
                </React.UI.Form.Item>

                {/* 邮箱验证码 */}
                <React.UI.Form.Item
                    label='邮箱验证码'
                    name='code'
                    rules={[{ required: true, message: '邮箱验证码不能为空' }]}
                    extra={
                        <CountDown
                            initialSeconds={10}
                            buttonTextBefore="发送验证码"
                            SendEmail={SendEmail}
                        />
                    }
                >
                    <React.UI.Input placeholder='请输入邮箱验证码' clearable />
                </React.UI.Form.Item>
            </React.UI.Form>
        </>
    )
}

export default Email;